package com.xxxx.server.pojo;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.xxxx.server.utils.CustomAuthorityDeserializer;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import org.apache.commons.lang.StringUtils;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;
import java.io.Serializable;
import java.sql.Timestamp;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
/**
 * <p>
 *
 * </p>
 *
 * @author zhoubin
 * @since 2020-10-15
 */
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@TableName("t_admin")
@ApiModel(value = "Admin对象", description = "")
@NoArgsConstructor //无参的构造方法
public class Admin implements Serializable, UserDetails {

	private static final long serialVersionUID = 1L;

	@ApiModelProperty(value = "id")
	@TableId(value = "id", type = IdType.AUTO)
	private Integer id;

	@ApiModelProperty(value = "姓名")
	private String name;
	/**
	 * 手机号码
	 */
	@ApiModelProperty(value = "手机号码")
	private String phone;

	@ApiModelProperty(value = "住宅电话")
	private String telephone;

	@ApiModelProperty(value = "联系地址")
	private String address;

	@ApiModelProperty(value = "是否启用")
	private Boolean enabled;

	@ApiModelProperty(value = "用户名")
	private String username;

	@ApiModelProperty(value = "密码")
	private String password;

	@ApiModelProperty(value = "用户头像")
	private String userFace;

	@ApiModelProperty(value = "是否已被删除")
	private Integer isActive;

	@ApiModelProperty(value = "备注")
	private String remark;

	@ApiModelProperty(value = "邮箱")
	private String email;

	@ApiModelProperty(value = "生日")
	private Timestamp birthday;

	@ApiModelProperty(value = "粉丝数")
	private Integer fansCount;
	@ApiModelProperty(value = "关注数")
	private Integer followCount;

	@ApiModelProperty(value = "性别")
	private String sex;

	@ApiModelProperty(value = "用户对应角色")
	@TableField(exist = false)
	private List<Role> roles;

	@ApiModelProperty(value = "获赞数")
	private Integer praiseCount;

	@ApiModelProperty(value = "我的地址")
	@TableField(exist = false)
	private List<String> myAddres;


//	@JsonDeserialize(using = CustomAuthorityDeserializer.class)
//	public void setAddress(String address) {
//		this.address = Arrays.asList(address.split("_"));
//	}

	public List<String> getMyAddres() {
		if (StringUtils.isBlank(address)){
			return null;
		}
		return Arrays.asList(address.split("_"));
	}

	@Override
	@JsonDeserialize(using = CustomAuthorityDeserializer.class)
	public Collection<? extends GrantedAuthority> getAuthorities() {
		if (roles!=null)
			return roles
				.stream()
				.map(role -> new SimpleGrantedAuthority(role.getName()))
				.collect(Collectors.toList());
		return null;
	}

	@Override
	public String getPassword() {
		return this.password;
	}

	@Override
	public String getUsername() {
		return this.username;
	}

	@Override
	public boolean isAccountNonExpired() {
		return true;
	}

	@Override
	public boolean isAccountNonLocked() {
		return true;
	}

	@Override
	public boolean isCredentialsNonExpired() {
		return true;
	}

	@Override
	public boolean isEnabled() {
		return enabled;
	}
}
